import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart' as fluro;
import 'package:logger/logger.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/live/allTag.dart';
import 'package:qgchat/bloc/live/anchor.dart';
import 'package:qgchat/bloc/live/live.dart';
import 'package:qgchat/bloc/live/ranking.dart';
import 'package:qgchat/bloc/msg/chat.dart';
import 'package:qgchat/bloc/msg/make.dart';
import 'package:qgchat/bloc/msg/msg.dart';
import 'package:qgchat/bloc/my/blacklistBloc.dart';
import 'package:qgchat/bloc/my/cashOut.dart';
import 'package:qgchat/bloc/my/cashOutRecordBloc.dart';
import 'package:qgchat/bloc/my/inviteProfitBloc.dart';
import 'package:qgchat/bloc/my/recharge.dart';
import 'package:qgchat/bloc/my/releaseVideoBloc.dart';
import 'package:qgchat/bloc/shortvideo/innerBloc.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/page/broadcast/LiveRoom/LiveRoom.dart';
import 'package:qgchat/page/broadcast/ReadyLive/ReadyLive.dart';
import 'package:qgchat/page/live/EndCall/EndCall.dart';
import 'package:qgchat/page/live/LodingCall/LodingCall.dart';
import 'package:qgchat/page/live/MakeCall/MakeCall.dart';
import 'package:qgchat/page/live/dynamic/index.dart';
import 'package:qgchat/page/live/talk/Talk.dart';
import 'package:qgchat/page/live/videoPlay/VideoPlay.dart';
import 'package:qgchat/page/my/Face/Face.dart';
import 'package:qgchat/page/my/coverVideo/CoverVideo.dart';
import 'package:qgchat/page/my/inviteProfit/index.dart';
import 'package:qgchat/page/my/myDynamic/index.dart';
import 'package:qgchat/page/my/myWechat/MyWechat.dart';
import 'package:qgchat/page/my/phoneLogin/index.dart';
import 'package:qgchat/page/my/manageAccount/index.dart';
import 'package:qgchat/page/my/releaseDynamic/index.dart';
import 'package:qgchat/page/my/releaseVideoDynamic/releaseVideoDynamic.dart';
import 'package:qgchat/page/my/releasevideo/index.dart';
import 'package:qgchat/page/my/uploadvideo/index.dart';
import 'package:qgchat/page/snap/convention/index.dart';
import 'package:qgchat/page/snap/rank/localtycoon.dart';

import 'package:qgchat/page/tabBar_page.dart';
import 'package:qgchat/page/live/nominate/index.dart';
import 'package:qgchat/page/live/newMan/index.dart';
import 'package:qgchat/page/live/tryToSee/index.dart';
import 'package:qgchat/page/live/onLine/index.dart';
import 'package:qgchat/page/live/search/index.dart';
import 'package:qgchat/page/live/anchorInfo/index.dart';
import 'package:qgchat/page/live/allTag/index.dart';
import 'package:qgchat/page/live/ranking/index.dart';
import 'package:qgchat/page/live/videoPage/live/index.dart';
import 'package:qgchat/page/live/videoPage/liveEnd/index.dart';
import 'package:qgchat/page/live/videoPage/answerPhone/index.dart';
import 'package:qgchat/page/msg/msg/index.dart';
import 'package:qgchat/page/msg/make/index.dart';
import 'package:qgchat/page/msg/call/index.dart';
import 'package:qgchat/page/msg/gold/index.dart';
import 'package:qgchat/page/msg/assess/index.dart';
import 'package:qgchat/page/msg/chat/index.dart';
import 'package:qgchat/page/my/attentionOrFans/index.dart';
import 'package:qgchat/page/my/attestationInfo/index.dart';
import 'package:qgchat/page/my/basicInfo/index.dart';
import 'package:qgchat/page/my/blacklist/index.dart';
import 'package:qgchat/page/my/login/index.dart';
import 'package:qgchat/page/my/forgetPsw/index.dart';
import 'package:qgchat/page/my/myWallet/index.dart';
import 'package:qgchat/page/my/recharge/index.dart';
import 'package:qgchat/page/my/rechargeRecord/index.dart';
import 'package:qgchat/page/my/register/index.dart';
import 'package:qgchat/page/my/inputCode/index.dart';
import 'package:qgchat/page/my/anchorAuth/index.dart';
import 'package:qgchat/page/my/inviteFriends/index.dart';
import 'package:qgchat/page/my/timeValue/index.dart';
import 'package:qgchat/page/my/inviteInfo/index.dart';
import 'package:qgchat/page/my/cashOut/index.dart';
import 'package:qgchat/page/my/cashOutRecord/index.dart';
import 'package:qgchat/page/my/video/index.dart';
import 'package:qgchat/page/video/giftranking/index.dart';
import 'package:qgchat/page/video/innerpage/index.dart';

class Routes {
  /// 路由配置
  /// 配置项：
  ///   widget 路由生产函数，包含传参和不传参两种，参数需要手动处理参数
  ///   isLogin 是否需要登录才能进的路由
  static Map routes = {
    // 首页tabbar
    "/": {
      "widget": (context, params) => AppBottomBars(),
    },

    // 推荐直播
    "/nominate": {
      "widget": (context, params) => Nominate(),
    },

    // 新人直播
    "/newMan": {
      "widget": (context, params) => NewMan(),
    },

    // 试看直播
    "/tryToSee": {
      "widget": (context, params) => TryToSee(),
    },

    // 在线直播
    "/onLine": {
      "widget": (context, params) => OnLine(),
    },

    // 绿色公约
    "/Convention": {
      "widget": (context, params) => Convention(),
    },

    // 土豪排行榜
    "/Localtycoon": {
      "widget": (context, params) => Localtycoon(),
    },

    // 搜索
    "/search": {
      "widget": (context, params) => Search(),
    },

    // 主播介绍页
    "/anchorInfo": {
      "widget": (context, params) {
        String token = deJson(params["token"]?.first);
        return BlocProvider(
          bloc: AnchorBloc(),
          child: AnchorInfo(token),
        );
      },
    },

    // 所有标签
    "/allTag": {
      "widget": (context, params) {
        int zid = deJson(params["id"]?.first);
        return BlocProvider(
          bloc: AllTagBloc(),
          child: AllTag(zid),
        );
      },
    },

    // 排行榜页
    "/ranking": {
      "widget": (context, params) {
        String memberToken = deJson(params["memberToken"]?.first);
        return BlocProvider(
          bloc: RankingBloc(memberToken),
          child: Ranking(memberToken),
        );
      },
    },

    // 通话页面
    "/live": {
      "widget": (context, params) {
        bool isAnchor = deJson(params["isAnchor"]?.first);
        bool isReceive = deJson(params["isReceive"]?.first);
        int targetInv = deJson(params["targetInv"]?.first);
        int userId = deJson(params["userId"]?.first);
        int anchorId = deJson(params["anchorId"]?.first);
        String targetName = deJson(params["targetName"]?.first);
        String targetHead = deJson(params["targetHead"]?.first);
        String targetToken = deJson(params["targetToken"]?.first);
        return BlocProvider(
          bloc: LiveBloc(
            inv: targetInv,
            aid: anchorId,
            uid: userId,
            targetToken: targetToken,
            targetName: targetName,
            targetHead: targetHead,
            isReceive: isReceive,
            isAnchor: isAnchor,
          ),
          child: Live(
            isAnchor,
            isReceive,
            targetInv,
            userId,
            anchorId,
            targetName,
            targetHead,
            targetToken,
          ),
        );
      },
    },

    // 通话结束
    "/liveEnd": {
      "widget": (context, params) => LiveEnd(),
    },

    // 收到电话
    "/answerPhone": {
      "widget": (context, params) => AnswerPhone(),
    },

    // 登录
    "/login": {
      "widget": (context, params) => Login(),
    },

    // 手机登录
    "/phoneLogin": {
      "widget": (context, params) => PhoneLogin(),
    },

    // 消息
    "/msg": {
      "widget": (context, params) {
        return BlocProvider(
          bloc: MsgBloc(),
          child: MsgIndex(),
        );
      },
    },

    // 预约主播
    "/make": {
      "widget": (context, params) {
        return BlocProvider(
          bloc: MakeBloc(),
          child: Make(),
        );
      },
    },

    // 通话记录
    "/call": {
      "widget": (context, params) => Call(),
    },

    // 金币记录
    "/gold": {
      "widget": (context, params) => Gold(),
    },

    // 评价列表
    "/assess": {
      "widget": (context, params) => Assess(),
    },

    // 私聊页面
    "/chat": {
      "widget": (context, params) {
        int targetId = deJson(params["targetId"]?.first);
        String targetName = deJson(params["targetName"]?.first);
        String targetHead = deJson(params["targetHead"]?.first);
        String targetToken = deJson(params["targetToken"]?.first);
        return BlocProvider(
          bloc: ChatInfoBloc(targetToken),
          child: Chat(
            targetId,
            targetName,
            targetHead,
            targetToken,
          ),
        );
      },
    },

    // 忘记密码
    "/forgetPsw": {
      "widget": (context, params) => ForgetPsw(),
    },

    // 注册
    "/register": {
      "widget": (context, params) => Register(),
    },

    // 小视频
    "/myvideo": {
      "widget": (context, params) => VideoInfo(),
    },
    // 发布的视频
    "/release": {
      "widget": (context, params) {
        return BlocProvider(
          bloc: ReleaseVideoBloc(),
          child: Releasevideo(),
        );
      },
    },
    // 小视频内页
    "/innervideo": {
      "widget": (context, params) {
        int id = deJson(params["id"]?.first);
        String image = deJson(params["image"]?.first);
        return BlocProvider(
          bloc: InnerBloc(id),
          child: Innerpage(image),
        );
      },
    },
    // 小视频礼物页
    "/giftranking": {
      "widget": (context, params) {
        int id = deJson(params["id"]?.first);
        return BlocProvider(
          bloc: InnerBloc(id),
          child: Giftranking(),
        );
      },
    },
    // 发布视频
    "/releasevideo": {
      "widget": (context, params) => Uploadvideo(),
    },

    // 输入验证码
    "/inputCode": {
      "widget": (context, params) {
        String phone = params['phone'].first;
        return InputCode(phone);
      },
    },

    // 认证信息
    "/attestation": {
      "widget": (context, params) => AttestationInfo(),
    },

    // 主播认证
    "/anchorAuth": {
      "widget": (context, params) {
        return AnchorAuth();
      }
    },

    // 编辑个性签名
//    "/sign": {
//      "widget": (context, params) => Sign(),
//    },

    // // 编辑形象标签
    // "/tag": {
    //   "widget": (context, params) => Tag(),
    // },

    // 关注or粉丝详情
    "/attentionOrFans": {
      "widget": (context, params) => AttentionOrFans(),
    },

    // 黑名单
    "/blacklist": {
      "widget": (context, params) {
        return BlocProvider(
          bloc: BlackListBloc(),
          child: Blacklist(),
        );
      },
    },

    // 基本资料
    "/basicInfo": {
      "widget": (context, params) => BasicInfo(),
    },

    // 邀请好友
    "/inviteFriends": {
      "widget": (context, params) => InviteFriends(),
    },
    // 时光价值
    "/timeValue": {
      "widget": (context, params) => TimeValue(),
    },

    // 我的钱包
    "/myWallet": {
      "widget": (context, params) => MyWallet(),
    },

    // 充值
    "/recharge": {
      "widget": (context, params) {
        return BlocProvider(
          bloc: RechargeBloc(),
          child: Recharge(),
        );
      },
    },

    // 充值记录
    "/rechargeRecord": {
      "widget": (context, params) => RechargeRecord(params['id']?.first),
    },

    // 邀请详情
    "/inviteInfo": {
      "widget": (context, params) => InviteInfo(),
    },

    // 邀请收益
    "/inviteProfit": {
      "widget": (context, params) {
        String token = deJson(params['token'].first);
        return BlocProvider(
          bloc: InviteProfitBloc(token),
          child: InviteProfit(),
        );
      },
    },

    // 提现
    "/cashOut": {
      "widget": (context, params) {
        return BlocProvider(
          bloc: CashOutBloc(),
          child: CashOut(),
        );
      },
    },

    // 提现记录
    "/cashOutRecord": {
      "widget": (context, params) {
        return BlocProvider(
          bloc: CashOutRecordBloc(),
          child: CashOutRecord(),
        );
      },
    },

    // 管理我的账号
    "/manageAccount": {
      "widget": (context, params) => ManageAccount(),
    },

    // 她的动态
    "/dynamic": {
      "widget": (context, params) {
        int id = deJson(params['id'].first);
        return Dynamic(id: id);
      },
    },

    // 我的动态
    "/myDynamic": {
      "widget": (context, params) => MyDynamic(),
    },

    // 发布动态
    "/releaseDynamic": {
      "widget": (context, params) => ReleaseDynamic(),
    },

    // 发布动态
    "/releaseVideoDynamic": {
      "widget": (context, params) => ReleaseVideoDynamic(),
    },

    // 视频播放
    "/videoPlay": {
      "widget": (context, params) {
        String url = deJson(params['url'].first);
        return VideoPlay(url);
      },
    },
   
    // 美颜设置
    "/myWechat": {
      "widget": (context, params) => MyWechat(),
    },
   
    // 选择封面视频
    "/coverVideo": {
      "widget": (context, params) => CoverVideo(),
    },

    // 通话界面
    "/talk": {
      "widget": (context, params) {
        String token = deJson(params['token'].first);
        int inv = deJson(params['inv'].first);
        return Talk(targetToken: token, inv: inv);
      },
    },

    // 收到电话页面
    "/makeCall": {
      "widget": (context, params) {
        int inv = deJson(params['inv'].first);
        String token = deJson(params['token'].first);
        return MakeCall(targetToken: token, inv: inv);
      },
    },

    // 通话结束页面
    "/endCall": {
      "widget": (context, params) {
        int inv = deJson(params['inv'].first);
        String token = deJson(params['token'].first);
        return EndCall(targetToken: token, inv: inv);
      },
    },

    // 发起通话邀请页面
    "/lodingCall": {
      "widget": (context, params) {
        String token = deJson(params['token'].first);
        return LodingCall(targetToken: token);
      },
    },
       
    // zego美颜设置
    "/face": {
      "widget": (context, params) => Face(),
    },
       
    // zego美颜设置
    "/readyLive": {
      "widget": (context, params) => ReadyLive(),
    },
       
    // 直播间
    "/liveRoom": {
      "widget": (context, params) {
        int id = deJson(params['id'].first);
        return LiveRoom(id);
      },
    },
  };

  /// 路由初始化
  static void configureRoutes(fluro.Router router) {
    // 空路由跳去哪里
    router.notFoundHandler = fluro.Handler(
      handlerFunc: (BuildContext context, Map<String, List<String>> params) {
        print("没有这个路由!");
        return null;
      },
    );

    routes.forEach((key, value) {
      router.define(key, handler: handlerBuild(key));
    });
  }

  /// 构建路由处理函数 - 路由拦截
  static fluro.Handler handlerBuild(String handlerName) {
    return fluro.Handler(
      handlerFunc: (BuildContext context, Map<String, List<String>> params) {
        var route = routes[handlerName];
        return route["widget"](context, params);
      },
    );
  }
}
